package com.tools.web.wx;

import com.tools.common.object.Note;

import java.util.Map;
import java.util.Objects;

/**
 * 微信登录所需配置参数的配置包装对象
 * */
@Note("微信登录所需配置参数的配置包装对象")
public class WxSource {

    @Note("微信登录所需的的 APPID，需要去微信开发者平台申请获得")
    private String appId;

    @Note("微信登录所需的的密钥，需要去微信开发者平台申请获得")
    private String secret;

    public WxSource() {}

    public static WxSource of() {
        return new WxSource();
    }

    @Note("读取 properties 里的配置项，转为实例返回")
    public static WxSource from(Map properties) {
        WxSource source = new WxSource();
        if(properties == null || properties.isEmpty()) return source;
        source.setAppId((String) properties.get("wx-login-appid"));
        source.setSecret((String) properties.get("wx-login-secret"));
        return source;
    }

    public String getAppId() {
        return appId;
    }

    public void setAppId(String appId) {
        this.appId = appId;
    }

    public String getSecret() {
        return secret;
    }

    public void setSecret(String appSecret) {
        this.secret = appSecret;
    }

    public boolean allNotEmpty() {
        return (appId != null && !appId.isEmpty()) &&
                (secret != null && !secret.isEmpty());
    }

    @Override
    public String toString() {
        return "WxSource{" +
                "appId=" + appId +
                ", secret=" + secret +
                '}';
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        WxSource wxSource = (WxSource) o;
        return Objects.equals(appId, wxSource.appId) && Objects.equals(secret, wxSource.secret);
    }

    @Override
    public int hashCode() {
        return Objects.hash(appId, secret);
    }
}
